package com.lemo.activemq_queues;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JmsProducer {

    public static void main(String[] args) throws JMSException {

        ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session;
        Destination destination;
        MessageProducer messageProducer;


        try {
            connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                    ActiveMQConnection.DEFAULT_PASSWORD,ActiveMQConnection.DEFAULT_BROKER_URL);
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("leo_o-queue");
            messageProducer = session.createProducer(destination);
            sendMsg(session, messageProducer);
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null)
                connection.close();
        }


    }

    private static void sendMsg(Session session, MessageProducer messageProducer) throws JMSException {
        for (int i = 0; i < 10000; i++) {
            TextMessage textMessage = session.createTextMessage(String.format("session send var i：{%s}", i));
            System.out.println(String.format("session send var i：{%s}", i));
            messageProducer.send(textMessage);

        }
    }
}
